package web.servlet;

import cn.dao.UserDao;
import cn.domain.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        String name = req.getParameter("name");
        String password = req.getParameter("password");
        User loginUser = new User();
        loginUser.setName(name);
        loginUser.setPassword(password);
        
//        Map<String,String[]> map = req.getParameterMap();
//        User loginUser = new User();
//        BeanUtils.populate(loginUser,map);

        UserDao dao = new UserDao();
        User user = dao.login(loginUser);
        if(user == null){
            //登陆失败
            req.getRequestDispatcher("/failServlet").forward(req,resp);
        }else{
            //登录成功
            req.setAttribute("user",user);
            req.getRequestDispatcher("/successServlet").forward(req,resp);


        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
